package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.PhotoActivity;
import edu.sctu.yueding.utils.CacheImgPathUtil;
import edu.sctu.yueding.utils.ImageLoader;
import edu.sctu.yueding.utils.JumpUtils;

/**
 * Created by 鑫爷您慢点 on 2016/8/3.
 */
public class PhotoSelectedGridViewAdapter extends BaseAdapter {
    private Activity activity;
    private List<String> mSelectedPhotoPath = new ArrayList<String>();
    private LayoutInflater mInflater;
    private int mScreenWidth;
    private EditText editText;

    public PhotoSelectedGridViewAdapter(Activity activity, List<String> mSelectedPhotoPath,EditText editText) {
        this.activity = activity;
        this.mSelectedPhotoPath = mSelectedPhotoPath;
        mInflater = LayoutInflater.from(activity);
        this.editText = editText;

        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;

    }


    @Override
    public int getCount() {
        if (mSelectedPhotoPath != null && mSelectedPhotoPath.size() > 0) {
            return mSelectedPhotoPath.size() + 1;
        } else {
            return 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return mSelectedPhotoPath.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.photo_selected_item_grid_view, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.mImg = (ImageView) convertView.findViewById(R.id.id_item_photo_img);
            viewHolder.mDelete = (ImageButton) convertView.findViewById(R.id.id_item_photo_img_btn);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.mImg.setImageResource(R.drawable.photo_pictures_no);
        viewHolder.mDelete.setImageResource(R.drawable.photo_delete_btn);
        viewHolder.mImg.setColorFilter(null);

        viewHolder.mImg.setMaxWidth(mScreenWidth / 4);

        if (mSelectedPhotoPath == null || mSelectedPhotoPath.size() == 0){
            setAddImgBtn(viewHolder);
        }else {
            if (position == mSelectedPhotoPath.size()){
                setAddImgBtn(viewHolder);
            }else {
                ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(mSelectedPhotoPath.get(position), viewHolder.mImg,false);
                viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position != mSelectedPhotoPath.size() ){
                            mSelectedPhotoPath.remove(position);
                            Set<String> set = new HashSet<String>();
                            CacheImgPathUtil cacheImgPathUtil = CacheImgPathUtil.getInstance(activity);
                            cacheImgPathUtil.removePathCache();
                            for (String s : mSelectedPhotoPath){
                                set.add(s);
                            }
                            cacheImgPathUtil.setCacheImgPath(set);
                            viewHolder.mImg.setImageBitmap(null);
                            viewHolder.mDelete.setImageBitmap(null);
                            notifyDataSetChanged();
                        }

                    }
                });
            }
        }
        return convertView;
    }

    /**
     * 显示 添加 图片
     * @param viewHolder
     */
    private void setAddImgBtn(ViewHolder viewHolder) {
        viewHolder.mImg.setImageResource(R.drawable.photo_add_icon);//323333
        viewHolder.mImg.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.mDelete.setVisibility(View.GONE);
        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setFocusable(false);
                Bundle bundle = new Bundle();
                bundle.putInt(PhotoActivity.FLAG, PhotoActivity.UPLOAD_TWEETS);
                JumpUtils.jumpActivity(activity, PhotoActivity.class, bundle);
                activity.finish();
            }
        });
    }

    private class ViewHolder {
        ImageView mImg;
        ImageButton mDelete;
    }
}
